﻿@using BlazorComponent.I18n;
@using BlazorToolkit;
@inherits LayoutComponentBase

<PageTitle>@Title</PageTitle>

<BlazorToolkitApp />

<MApp>
    <MAppBar App="true" Outlined="true" Elevation="0">
        @if (_showBack)
        {
            <MButton Icon="true" OnClick="Back">
                <MIcon>mdi-arrow-left</MIcon>
            </MButton>
        }
        else
        {
            <MAppBarNavIcon @onclick="() => _drawer = !_drawer"></MAppBarNavIcon>
        }

        <MToolbarTitle>@Title</MToolbarTitle>
        <MSpacer></MSpacer>
    </MAppBar>

    <MNavigationDrawer @bind-Value="_drawer"
                       App="true"
                       Floating="true"
                       Class="m-sheet--outlined">
        <MList Nav Routable>
            <MListItemGroup>
                <MListItem Href="">
                    <MListItemIcon>
                        <MIcon>mdi-weather-cloudy</MIcon>
                    </MListItemIcon>
                    <MListItemContent>
                        <MListItemTitle>天气</MListItemTitle>
                    </MListItemContent>
                </MListItem>
                <MListItem Href="location">
                    <MListItemIcon>
                        <MIcon>mdi-map-marker-outline</MIcon>
                    </MListItemIcon>
                    <MListItemContent>
                        <MListItemTitle>位置</MListItemTitle>
                    </MListItemContent>
                </MListItem>
                <MListItem Href="setting">
                    <MListItemIcon>
                        <MIcon>mdi-cog-outline</MIcon>
                    </MListItemIcon>
                    <MListItemContent>
                        <MListItemTitle>设置</MListItemTitle>
                    </MListItemContent>
                </MListItem>
            </MListItemGroup>
        </MList>
    </MNavigationDrawer>

    <MMain>
        <MContainer Fluid>
            <MErrorHandler>
                <CascadingValue Value="this" IsFixed>
                    @Body
                </CascadingValue>
            </MErrorHandler>
        </MContainer>
    </MMain>
</MApp>


@code {

    private bool _drawer;
    private bool _showBack;
    private string? Title;
    private string? HistoryUrl;

    [Inject]
    private MasaBlazor MasaBlazor { get; set; } = default!;
    [Inject]
    private IThemeService ThemeService { get; set; } = default!;
    [Inject]
    private ISettingsService SettingsService { get; set; } = default!;
    [Inject]
    private NavigationManager Navigation { get; set; } = default!;

    public void UpdateTitle(string? value)
    {
        Title = value;
        InvokeAsync(StateHasChanged);
    }

    public void ShowBack(bool show, string url)
    {
        _showBack = show;
        HistoryUrl = url;
        InvokeAsync(StateHasChanged);
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        ThemeService.OnChanged += ThemeChanged;
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await LoadSettings();
        }
        await base.OnAfterRenderAsync(firstRender);
    }

    private void ThemeChanged(ThemeType value)
    {
        if (MasaBlazor.Theme.Dark != (value == ThemeType.Dark))
        {
            MasaBlazor.ToggleTheme();
        }
    }

    private async Task LoadSettings()
    {
        int themeType = await SettingsService.Get<int>(SettingType.Theme);
        await ThemeService.SetThemeType((ThemeType)themeType);
    }

    private void Back()
    {
        _showBack = false;
        if (!string.IsNullOrEmpty(HistoryUrl))
        {
            Navigation.NavigateTo(HistoryUrl);
        }
    }
}
